import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index'

import Login from '@/view/login/login'

// 流程管理
import ProcessManagement from '@/view/process-management/process-list'
import Audit from '@/view/audit/audit'

// 个人中心
import Info from '@/view/user-center/info'
import Password from '@/view/user-center/password'

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace (location) {
  return originalReplace.call(this, location).catch(err => err)
}

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/view/login/register')
    },
    {
      path: '/audit-process',
      name: 'auditProcess',
      component: () => import('@/view/audit/audit-process')
    },
    {
      path: '/bidding-audit-process',
      name: 'biddingAuditProcess',
      component: () => import('@/view/process-management/bidding-scheme/audit/audit-process')
    },
    {
      path: '/home',

      redirect: '/homepage',
      component: () => import('@/view/home'),
      children: [
        {
          path: '/homepage',
          name: 'homepage',
          meta: [{ title: '首页' }],
          component: () => import('@/view/homepage/homepage')
        },
        {
          path: '/audit',
          name: 'audit',
          component: Audit,
          meta: [
            { title: '实施方案' },
            { title: '我的待办' },
          ],
        },
        // 流程管理
        {
          path: '/process-management/process-list',
          name: '流程管理',
          meta: [
            { title: '流程管理' },
            { title: '新建流程' },
          ],
          component: ProcessManagement
        },
        {
          path: '/create-embodiment',
          name: 'createEmbodiment',
          meta: [
            { title: '流程管理' },
            { title: '新建实施方案' },
          ],
          component: () => import('@/view/process-management/processlist/embodiment')
        },
        {
          path: '/create-designing',
          name: 'createDesigning',
          meta: [
            { title: '流程管理' },
            { title: '新建设计方案' },
          ],
          component: () => import('@/view/process-management/processlist/designing')
        },
        {
          path: '/create-bidding',
          name: 'createBidding',
          meta: [
            { title: '流程管理' },
            { title: '新建招投标流程' },
          ],
          component: () => import('@/view/process-management/processlist/bidding')
        },
        // 实施方案
        {
          path: '/implement-sponsor',
          name: 'implementSponsor',
          component: () => import('@/view/process-management/implement-scheme/sponsor/sponsor'),
          meta: [
            { title: '实施方案' },
            { title: '我的发起' },
          ],
        },
        {
          path: '/implement-process-status',
          name: 'implementProcessStatus',
          component: () => import('@/view/process-management/implement-scheme/process-status/list'),
          meta: [
            { title: '实施方案' },
            { title: '流程进度' },
          ],
        },
        // 设计方案
        {
          path: '/design-sponsor',
          name: 'designSponsor',
          component: () => import('@/view/process-management/design-scheme/sponsor/sponsor'),
          meta: [
            { title: '设计方案' },
            { title: '我的发起' },
          ],
        },
        {
          path: '/design-my-to-do',
          name: 'designMyToDo',
          component: () => import('@/view/process-management/design-scheme/my-to-do/index'),
          meta: [
            { title: '设计方案' },
            { title: '我的待办' },
          ],
        },
        {
          path: '/design-process-status',
          name: 'designProcessStatus',
          component: () => import('@/view/process-management/design-scheme/process-status/list'),
          meta: [
            { title: '设计方案' },
            { title: '流程进度' },
          ],
        },
        // 招投标流程
        {
          path: '/bidding-sponsor',
          name: 'biddingSponsor',
          component: () => import('@/view/process-management/bidding-scheme/sponsor/sponsor'),
          meta: [
            { title: '招投标流程' },
            { title: '我的发起' },
          ],
        },
        {
          path: '/bidding-my-to-do',
          name: 'biddingMyToDo',
          component: () => import('@/view/process-management/bidding-scheme/my-to-do/list'),
          meta: [
            { title: '招投标流程' },
            { title: '我的待办' },
          ],
        },
        {
          path: '/bidding-process-status',
          name: 'biddingProcessStatus',
          component: () => import('@/view/process-management/bidding-scheme/process-status/list'),
          meta: [
            { title: '招投标流程' },
            { title: '流程进度' },
          ],
        },
        // 单位管理
        {
          path: '/people',
          name: 'people',
          meta: [
            { title: '单位管理' },
            { title: '人员管理' },
          ],
          component: () => import('@/view/personnel/people')
        },
        {
          path: '/job',
          name: 'job',
          meta: [
            { title: '单位管理' },
            { title: '职位管理' },
          ],
          component: () => import('@/view/personnel/job/job-list')
        },
        {
          path: '/system-node-table',
          name: 'systemNodeTable',
          meta: [
            { title: '单位管理' },
            { title: '系统节点表' },
          ],
          component: () => import('@/view/personnel/system-node-table/list')
        },
        // 流程信息配置
        {
          path: '/process-config/projectType',
          name: 'projectType',
          meta: [
            { title: '流程信息配置' },
            { title: '项目类型配置' },
          ],
          component: () => import('@/view/process-config/projectType/list'),
        },
        {
          path: '/notice/list',
          name: 'notice',
          component: Info,
          meta: [
            { title: '通知管理' }
          ],
          component: () => import('@/view/notice/list')
        },
        {
          path: '/notice-user/list',
          name: 'noticeUser',
          component: Info,
          meta: [
            { title: '通知公告' }
          ],
          component: () => import('@/view/notice-user/list')
        },
        {
          path: '/user-center/info',
          name: 'info',
          component: Info,
          meta: [
            { title: '个人中心' },
            { title: '个人基本信息' },
          ],
        },
        {
          path: '/user-center/password',
          name: 'password',
          component: Password,
          meta: [
            { title: '个人中心' },
            { title: '修改密码' },
          ],
        },
        // 测试页面
        {
          path: '/z-test/one',
          component: () => import('@/view/z-test/one'),
        },
        {
          path: '/z-test/two',
          component: () => import('@/view/z-test/two'),
        },
      ]
    },
    {
      path: '/*',
      component: () => import('@/view/error-page/404')
    },
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path == '/') return next()
  if (to.path == '/register') return next()
  let token = sessionStorage.getItem('token')
  if (!token) return next('/')
  if (!store.state.logined) {
    store.dispatch('getMemberInfo').then(() => {
      return next()
    }).catch(() => {
      return next('/')
    })
  } else {
    next()
  }
})

export default router
